<template>
  <div>
    <el-dialog
      :title="titlecom"
      :visible.sync="dialogFormVisible"
      width="600px"
    >
      <!-- :rules="rules" -->
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="轮播图" :label-width="formLabelWidth" prop="image">
          <el-upload
            class="avatar-uploader"
            action=""
            :http-request="comodityUpload"
            :show-file-list="false"
            :limit="1"
          >
            <img v-if="form.image" :src="form.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="form.image" alt="" />
          </el-dialog>
          <!-- <div class="lunboTag">建议尺寸：400*400</div> -->
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="form.status" placeholder="请选择轮播图类型">
            <el-option
              v-for="a in reqCategory1"
              :key="a.id"
              :label="a.name"
              :value="a.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="quedingtianjia()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addbanneradd, updatebannerupdate } from "@/api/couponlist";
import { getservetypes } from "@/api/server";
import { upload } from "@/api/editor.js";
import EditorBar from "@/components/Editor";
import draggable from "vuedraggable";
export default {
  components: {
    EditorBar,
    draggable,
  },
  data() {
    return {
      reqCategory1: [
        { id: 1, name: "文案页" },
        { id: 2, name: "批发页" },
        { id: 3, name: "耗材页" },
      ],
      dialogVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "150px",
      form: {
        id: "",
        image: "",
        status: "",
      },
      imgList: [],
      dialogImageUrl: "",
      dialogVisible1: false,
      rules: {
        image: [{ required: true, message: "请上传图片", trigger: "blur" }],
        status: [{ required: true, message: "请选择类型", trigger: "blur" }],
      },
      qubie: 0,
    };
  },
  methods: {
    //轮播图上传
    uploadFile(param) {
      var file = param.file;
      const formData = new FormData();
      formData.append("file", file);
      upload(formData, param).then((res) => {
        if (res.code == 0) {
          // ---------------------------这是商品轮播图 最后要通过 图片逗号拼接
          this.imgList.push({ url: res.data });
        }
      });
      console.log(this.imgList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible1 = true;
      console.log(file);
    },
    //图片移除图片
    handleRemove(file, fileList) {
      // fileList是剩下的照片
      this.imgList = fileList;
    },
    //图上传
    async comodityUpload(param) {
      var file = param.file;
      const formData = new FormData();
      formData.append("file", file);
      console.log(param);
      // let result = await upload(formData);
      // console.log(result);
      // if (res.code == 0) {
      //   this.form.image = res.data;
      // }
      upload(formData, param).then((res) => {
        console.log(res);
        if (res.code == 0) {
          this.form.image = res.data;
        }
      });
    },

    add() {
      this.form = {
        image: "",
        status: "",
      };
      this.qubie = 0;

      this.dialogFormVisible = true;
      this.imgList = [];
    },
    edit(val) {
      this.form = {
        image: "",
        status: "",
      };
      this.qubie = 1;

      this.form = Object.assign(this.form, val);
      this.dialogFormVisible = true;
      this.imgList = [];
    },
    // 关闭
    handleClose() {
      this.dialogFormVisible = false;
      this.$emit("refresh");
    },

    async quedingtianjia() {
      if (this.qubie == 0) {
        let param = {
          image: this.form.image,
          status: this.form.status,
        };
        let result = await addbanneradd(param);
        if (result.code == 0) {
          this.$message({
            message: `添加图片成功`,
            type: "success",
          });
          this.handleClose();
        }
      } else if (this.qubie == 1) {
        let param = {
          id: parseInt(this.form.id),
          image: this.form.image,
          status: this.form.status,
        };
        let result = await updatebannerupdate(param);
        if (result.code == 0) {
          this.$message({
            message: `修改图片成功`,
            type: "success",
          });
          this.handleClose();
        }
      }
    },
  },
  watch: {
    imgList: {
      handler(newVal) {
        // console.log("监听", newVal);
        // 这里创建新的数组 用逗号进行隔开 主要是接口要求
        let imagesarr = [];
        const result = newVal.map((item) => `${item.url}`).join(",");
        // this.form.content = result;
        console.log("猪嗨", this.form.content);
      },
      deep: true,
    },
  },
  computed: {
    titlecom() {
      if (this.qubie == 0) {
        return "添加图片";
      } else {
        return "修改图片";
      }
    },
  },
};
</script>

<style scoped lang="scss">
.specificationInput {
  width: 250px;
}
.avatar-uploader-icon[data-v-22a48a68] {
  // border: 1px dashed #d9d9d9;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  flex-direction: column;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 300px;
  height: 300px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.specificationInput {
  width: 250px;
}

.addguigeBtn {
  margin-left: 100px;
  margin-bottom: 22px;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 300px;
  height: 300px;
  line-height: 300px;
  text-align: center;
  border: 1px dashed #c0ccda;
}

.avatar {
  width: 300px;
  height: 300px;
  display: block;
}
v-deep .el-upload--picture-card {
  width: 200px !important;
  height: 200px !important;
}
.specificationInput1 {
  width: 150px;
}
</style>
